

# this is the most powerful comand in the sed. the substitude.
# basic syntax:
# sed '[addresss-range|pattern-range] s/original-string/replacement-string/[substitude-flags]' input-file
#
# - address-range:      optinal, substitude range
# - pattern-range:      optinal, only do substitude on lines matching pattern-range
# - s:                  tells sed to execute the substitude command 
# - original-string:    the string to be searched for in the input file. support regular expression
# - replacement-string: sed will repalce original-string with this string
# - substitude-flags:   optinal

# NOTE: remember sed do not edit original file, the substitution takes place in the pattern space buffer




#-- regular usage -- 
# - replace all occurrences of Manager with Director
sed 's/Manager/Director/' ./test_employee.txt # no any range specified. sed try to do substitude on all lines
echo -e '\n'

# - replace Manager with Director only on lines that contain the keyword 'Sales'

sed '/Sales/s/Manager/Director/' ./test_employee.txt # pattern-range specified.
echo -e "\n"

sed -n '/Sales/s/Manager/Director/ p' ./test_employee.txt # only prints target 
echo -e '\n';




#-- global flag -- 
# by default sed substitude command will replace only 1st occurrence of the {original-string} on each line.
# if we want to substitude all occurrences of {original-string} ,we should use global flag 'g'

# - replace the 1st occurrence of lower case 'a' with upper case A:
sed 's/a/A/' ./test_employee.txt # only replace 2nd line 'name' to 'nAme'
echo -e '\n'

# - replace all occurrences of lower case 'a' with upper case 'A':
sed 's/a/A/g' ./test_employee.txt
echo -e '\n'




#-- number flag --
# use the number flag to specify a secific occurrences of the original-string. 
# only n-th instance of original-string will trigger the substitution.
# NOTE: range between 1 to 512

# - replace the 2nd occurrence of lower case 'a' to upper case A:
sed 's/a/A/2' ./test_employee.txt
echo -e '\n'
#   eg: 102,Jason Smith,IT Manager -> 102,Jason Smith,IT MAnager. replace the 2nd occurrence 

# - change only 2rd occurrence of 'locate' to 'find' 
sed 's/locate/find/2' ./test_substitude-locate.txt
echo -e '\n'




#-- print flags -- 
# when substitude is successful, use 'p' flag to print the changed line. use -n option to cancel defualt sed
# printing behaviour

# - print only the line that was changed by the substiude command 
sed -n 's/John/Johnny/p' ./test_employee.txt
echo -e '\n'




#-- write flag --
# we can use 'w' flag to write substitude result to file when substitution is successful.

# - write only the line that was changed by the substitude command to output.txt
sed -n 's/John/Johnny/w output.txt' test_employee.txt
#   if there are no occurrence line, sed will create an empty file.




#-- Ignore case flags --
# we can use 'i' flag to match the original-string in a case-insensitive mannar.
# NOTE: only avaliable GNU sed.

# - replace "john" with "Johnny"
sed 's/john/Johnny/' test_employee.txt
echo -e '\n'

# - replace "john" or "John" with "Johnny"
sed 's/john/Johnny/i' ./test_employee.txt
echo -e '\n'




#-- Execute flag -- 
# using the sed 'e' flag, you can execute whatever is available in the pattern space as a shell command,
# and ouptut will be returned to the pattern space.
# NOTE: only GNU sed

# - add the text 'ls -l' in front of every line in the test_files.txt and print the output
sed 's/^/ls -l /' ./test_file.txt # note the space between options and arguments
echo -e '\n'

# - add 'ls -l' in front of every line in the files.txt and execute the output
sed 's/^/ls -l /e' ./test_file.txt
echo -e '\n'




#-- Delimiter --
# In all above sed examples, we used the defualt sed delimiter '/'. when there is a slash '/' in the
# original-string or the replacement-string, we need to escape it use '\' back-slash, such as modify path

# change /usr/local/bin to /usr/bin
sed 's/\/usr\/local\/bin/\/usr\/bin/' ./test_path.txt
echo -e '\n'




#-- Multiple Substitude -- 
# use '{}' embraces 's' sed command to operate multiple substitude operation on one line.

# - change 'Developer' to 'IT Manager', then change 'Manager' to 'Director':
sed '{
    s/Developer/IT Manager/
    s/Manager/Director/
}' ./test_employee.txt
# NOTE: sed execute 2nd substitude command over 1st operation result.
